//
//  JHHotUserViewController.m
//  Liberty Voice
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 ClearLove. All rights reserved.
//

#import "JHHotUserViewController.h"
#import "JHHotUserMoveViewController.h"
#import "JHHotUserTableViewCell.h"
#import "NetwordHandler.h"
#import "MJRefresh.h"

@interface JHHotUserViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *HotUserTable;
@property(nonatomic,retain)NSMutableArray *hotUserArr;
@property(nonatomic,retain)NSMutableArray *Arr;
@property(nonatomic,retain)NSString *str;

@end

@implementation JHHotUserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"热门主播";
       self.hotUserArr = [NSMutableArray array];
    [self createHotUser];
    [self addHeader];
    [self addFooter];
   
}
/**
 *  下拉刷新
 */
- (void)addHeader
{
    __block JHHotUserViewController *hotUser = self;
    [self.HotUserTable addHeaderWithCallback:^{
        hotUser.isUpLoading = NO;
        [hotUser GetPersonList:1];//重新请求数据
    }];
    [self.HotUserTable headerBeginRefreshing];
}

- (void)addFooter
{
    __block JHHotUserViewController *hotUser = self;
    [self.HotUserTable addFooterWithCallback:^{
        hotUser.isUpLoading = YES;
        [hotUser GetPersonlist:(hotUser.nextCursor++)+1];
    }];
    
    [self.HotUserTable footerBeginRefreshing];

}
- (void)GetPersonList:(NSInteger)nextCursor
{

    /**
     *  拼接页数
     */
    NSString *url_string = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_user_index?device=android&page=%ld",nextCursor];
    // 获取网络数据
    [NetwordHandler requestWithURL:url_string completion:^(id result) {
        NSDictionary *dic = result;
        NSLog(@"%@",result);
        if (self.isUpLoading == NO) {
            //说明是下拉刷新，就要清空数组中的数据，保持数组中存放的是最新的数据
            [self.hotUserArr removeAllObjects];
        }
     
        NSMutableArray *a = [NSMutableArray array];
        a = [dic objectForKey:@"list"];
        for (NSDictionary *d in a) {
            JHHotUser *u = [[JHHotUser alloc] initWithDic:d];
            NSLog(@"%@",u.list);
            [_hotUserArr addObject:u];
        }
        [self.HotUserTable headerEndRefreshing];
        [self.HotUserTable footerEndRefreshing];
        [self.HotUserTable reloadData];
        
    }];
    
}
- (void)GetPersonlist:(NSInteger)nextCursor
{
    if (nextCursor <= 3) {
        
        
        /**
         *  拼接页数
         */
        NSString *url_string = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_user_index?device=android&page=%ld",nextCursor];
        // 获取网络数据
        [NetwordHandler requestWithURL:url_string completion:^(id result) {
            NSDictionary *dic = result;
            NSLog(@"%@",result);
      
 
            NSMutableArray *a = [NSMutableArray array];
            a = [dic objectForKey:@"list"];
            for (NSDictionary *d in a) {
                JHHotUser *u = [[JHHotUser alloc] initWithDic:d];
                NSLog(@"%@",u.list);
                [_hotUserArr addObject:u];
            }
 
            [self.HotUserTable footerEndRefreshing];
            [self.HotUserTable reloadData];
            
        }];}
    
}

- (void)createHotUser
{
    
    
    self.HotUserTable = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 50) style:UITableViewStyleGrouped] autorelease];
    _HotUserTable.delegate = self;
    _HotUserTable.dataSource = self;
    [_HotUserTable registerClass:[JHHotUserTableViewCell class] forCellReuseIdentifier:@"HotUser"];
    [self.view addSubview:_HotUserTable];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JHHotUserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HotUser"];
    cell.hotUser = _hotUserArr[indexPath.section];
    cell.ablumView = self;
     return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _hotUserArr.count;
//    NSLog(@"%d",_hotUserArr.count);
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    v.backgroundColor = [UIColor whiteColor];
    UILabel *l =[[UILabel alloc] initWithFrame:CGRectMake(20, 0, v.frame.size.width - 80, v.frame.size.height)];
//    NSLog(@"%@",[[_hotUserArr objectAtIndex:section]objectForKey:@"title"]);
    l.text = [_hotUserArr[section] title];
    [v addSubview:l];
    UIButton *moveButton = [UIButton buttonWithType:UIButtonTypeSystem];
    moveButton.frame = CGRectMake(l.frame.size.width, 0,v.frame.size.width - l.frame.size.width, v.frame.size.height);
    [moveButton setTitle:@"更多>>" forState:UIControlStateNormal];
    moveButton.tag = section + 10000;
//    self.str = [_hotUserArr[section] name];
    [moveButton addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside];
    [v addSubview:moveButton];

    return v;
}
- (void)move:(UIButton *)button
{
    NSLog(@"%ld",button.tag - 10000);
    JHHotUserMoveViewController *jh = [[JHHotUserMoveViewController alloc] init];
    jh.name = [[_hotUserArr objectAtIndex:button.tag - 10000] name];
    [self.navigationController pushViewController:jh animated:YES];
    NSLog(@"点击");
}
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    [tableView deselectRowAtIndexPath:indexPath animated:NO];
//    JHHotUserMoveViewController *move = [[JHHotUserMoveViewController alloc] init];
//    move.
//    [self.navigationController pushViewController:move animated:YES];
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
